package pl.edu.mimuw.irs.rcp.controller.editor.workflow.command;

import org.eclipse.draw2d.Bendpoint;
import org.eclipse.draw2d.Connection;
import org.eclipse.draw2d.RelativeBendpoint;
import org.eclipse.draw2d.geometry.Dimension;
import org.eclipse.draw2d.geometry.Point;
import org.eclipse.gef.commands.Command;
import org.eclipse.gef.requests.BendpointRequest;

import pl.edu.mimuw.irs.core.model.petrinet.Arc;
import pl.edu.mimuw.irs.core.util.RelativePosition;
import pl.edu.mimuw.irs.rcp.controller.editor.workflow.part.ArcEditPart;

public class AbstractBendpointCommand extends Command {

	// *** Attributes

	private int index;
	private Point point;
	private Bendpoint bendpoint;
	private RelativePosition position;
	private Arc arc;
	private Dimension d1;
	private Dimension d2;
	// Getters and setters
	public int getIndex() {
		return index;
	}
	public void setIndex(int index) {
		this.index = index;
	}
	public Point getPoint() {
		return point;
	}
	public void setPoint(Point point) {
		this.point = point;
	}
	public Bendpoint getBendpoint() {
		return bendpoint;
	}
	public void setBendpoint(Bendpoint bendpoint) {
		this.bendpoint = bendpoint;
	}
	public RelativePosition getPosition() {
		return position;
	}
	public void setPosition(RelativePosition position) {
		this.position = position;
	}
	public Arc getArc() {
		return arc;
	}
	public void setArc(Arc arc) {
		this.arc = arc;
	}
	public Dimension getD1() {
		return d1;
	}
	public void setD1(Dimension d1) {
		this.d1 = d1;
	}
	public Dimension getD2() {
		return d2;
	}
	public void setD2(Dimension d2) {
		this.d2 = d2;
	}
	
	// *** Constructors
	
	public AbstractBendpointCommand(BendpointRequest request) {
		setIndex(request.getIndex());
		setPoint(request.getLocation().getCopy());
		setArc((Arc) request.getSource().getModel());
		ArcEditPart arcEditPart = (ArcEditPart) request.getSource();
		Connection connection = arcEditPart.getConnectionFigure();
		RelativeBendpoint relative = new RelativeBendpoint(connection);
		relative.setConnection(connection);
		connection.translateToRelative(getPoint());
		
		Point ref1 = connection.getSourceAnchor().getReferencePoint();
		Point ref2 = connection.getTargetAnchor().getReferencePoint();
		connection.translateToRelative(ref1);
		connection.translateToRelative(ref2);

		d1 = getPoint().getDifference(ref1);
		d2 = getPoint().getDifference(ref2);
		
		relative.setRelativeDimensions(d1, d2);
		setBendpoint(relative);
		setPosition(new RelativePosition(d1.width, d1.height, d2.width, d2.height));
	}
}
